package com.inei.evaluacionemype.controller.activity;

import com.inei.evaluacionemype.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class CapituloIV_ParteI_Activity extends Activity {

	// componentes
	private EditText etRuc, etInf, etTel, etMov, etRed, etInte, etC4_21,
			etC4_22, etC4_3;
	private RadioButton rbInf1, rbInf2, rbTel1, rbTel2, rbMov1, rbMov2, rbRed1,
			rbRed2, rbInte1, rbInte2, rbIntr1, rbIntr2, rbExtr1, rbExtr2;

	// variables

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_capitulo_iv_parte_1);

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.capitulo_iv_1, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnGuardar:
			String resultado = Validar();
			// if (resultado.equals("")) {
			if (true) {
				GuardarDatos();
			} else {
				Toast.makeText(this, resultado, Toast.LENGTH_SHORT).show();
			}

			break;

		case R.id.mnSalir:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_cap3 = new Intent(this, CapituloIII_Activity.class);
		ir_cap3.putExtras(getIntent().getExtras());
		startActivity(ir_cap3);
		finish();
		super.onBackPressed();
	}

	private void IniciarComponentes() {
		etRuc = (EditText) findViewById(R.id.etRuc);
		etInf = (EditText) findViewById(R.id.etInf);
		etTel = (EditText) findViewById(R.id.etTel);
		etMov = (EditText) findViewById(R.id.etMov);
		etRed = (EditText) findViewById(R.id.etRed);
		etInte = (EditText) findViewById(R.id.etInte);
		etC4_21 = (EditText) findViewById(R.id.etC4_21);
		etC4_22 = (EditText) findViewById(R.id.etC4_22);
		etC4_3 = (EditText) findViewById(R.id.etC4_3);

		rbInf1 = (RadioButton) findViewById(R.id.rbInf1);
		rbInf2 = (RadioButton) findViewById(R.id.rbInf2);
		rbTel1 = (RadioButton) findViewById(R.id.rbTel1);
		rbTel2 = (RadioButton) findViewById(R.id.rbTel2);
		rbMov1 = (RadioButton) findViewById(R.id.rbMov1);
		rbMov2 = (RadioButton) findViewById(R.id.rbMov2);
		rbRed1 = (RadioButton) findViewById(R.id.rbRed1);
		rbRed2 = (RadioButton) findViewById(R.id.rbRed2);
		rbInte1 = (RadioButton) findViewById(R.id.rbInte1);
		rbInte2 = (RadioButton) findViewById(R.id.rbInte2);
		rbIntr1 = (RadioButton) findViewById(R.id.rbIntr1);
		rbIntr2 = (RadioButton) findViewById(R.id.rbIntr2);
		rbExtr1 = (RadioButton) findViewById(R.id.rbExtr1);
		rbExtr2 = (RadioButton) findViewById(R.id.rbExtr2);

		etInf.setEnabled(false);
		etTel.setEnabled(false);
		etMov.setEnabled(false);
		etRed.setEnabled(false);
		etInte.setEnabled(false);
	}

	private void AgregarEventos() {
		EventoSiNo(rbInf1);
		EventoSiNo(rbInf2);
		EventoSiNo(rbTel1);
		EventoSiNo(rbTel2);
		EventoSiNo(rbMov1);
		EventoSiNo(rbMov2);
		EventoSiNo(rbRed1);
		EventoSiNo(rbRed2);
		EventoSiNo(rbInte1);
		EventoSiNo(rbInte2);
	}

	private void EventoSiNo(final RadioButton rb) {
		rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (rb == rbInf1 || rb == rbInf2) {
					etInf.requestFocus();
					etInf.setEnabled(rbInf1.isChecked() ? true : false);
					etInf.setText(rbInf2.isChecked() ? "" : null);
				}
				if (rb == rbTel1 || rb == rbTel2) {
					etTel.requestFocus();
					etTel.setEnabled(rbTel1.isChecked() ? true : false);
					etTel.setText(rbTel2.isChecked() ? "" : null);
				}
				if (rb == rbMov1 || rb == rbMov2) {
					etMov.requestFocus();
					etMov.setEnabled(rbMov1.isChecked() ? true : false);
					etMov.setText(rbMov2.isChecked() ? "" : null);
				}
				if (rb == rbRed1 || rb == rbRed2) {
					etRed.requestFocus();
					etRed.setEnabled(rbRed1.isChecked() ? true : false);
					etRed.setText(rbRed2.isChecked() ? "" : null);
				}
				if (rb == rbInte1 || rb == rbInte2) {
					etInte.requestFocus();
					etInte.setEnabled(rbInte1.isChecked() ? true : false);
					etInte.setText(rbInte2.isChecked() ? "" : null);
				}
			}
		});
	}

	private void CargarDatos() {

	}

	private void GuardarDatos() {
		if (rbInte1.isChecked()) {
			Intent ir_cap4_2 = new Intent(this,
					CapituloIV_ParteII_Activity.class);
			startActivity(ir_cap4_2);
			finish();
		} else {
			Intent ir_cap4_3 = new Intent(this,
					CapituloIV_ParteIII_Activity.class);
			startActivity(ir_cap4_3);
			finish();
		}
	}

	private String Validar() {
		return "";
	}
}
